package com.pgcomb.poweriot.alarmservice.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pgcomb.poweriot.alarmservice.alarm.service.MqttAlarmService;
import com.pgcomb.poweriot.alarmservice.vo.AlarmMqtt;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhengheqi
 * @version 1.0
 * @description: 告警消息消费
 * @date 2021/5/29 18:52
 */
@Slf4j
//@Component
public class AlarmMqttReceive {
    @Resource
    ObjectMapper objectMapper;
    @Resource
    MqttAlarmService mqttAlarmService;

//    @RabbitListener(queues = "sendMqqtMsg")
    public void receiveMessage(Message message, Channel channel){
        Object payload = message.getPayload();
        //	1. 收到消息以后进行业务端消费处理
        log.info("消费消息:{}", payload);
        AlarmMqtt alarmData;
        try {
            alarmData = objectMapper.readValue(objectMapper.writeValueAsString(payload), AlarmMqtt.class);
        } catch (JsonProcessingException e) {
            log.error("alarm message transform error", e);
            return;
        }
        mqttAlarmService.send(alarmData);
    }
}
